#include "leaverecordview.h"
#include <QMessageBox>
#include <QDebug>

LeaveRecordView::LeaveRecordView(AdminManager *adminManager, QWidget *parent) : QWidget(parent),
    m_adminManager(adminManager)
{
    initUI();
    loadPendingLeaveRequests();

    // 连接信号槽
    connect(m_adminManager, &AdminManager::operationResult, this, [=](bool success, const QString &message) {
        if (success) {
            QMessageBox::information(this, "操作成功", message);
            loadPendingLeaveRequests(); // 刷新列表
        } else {
            QMessageBox::critical(this, "操作失败", message);
        }
    });
}

void LeaveRecordView::initUI()
{
    // 设置窗口标题
    setWindowTitle("请假记录审核");
    setMinimumSize(800, 600);

    // 创建主布局
    QVBoxLayout *mainLayout = new QVBoxLayout(this);

    // 创建标题标签
    QLabel *titleLabel = new QLabel("待审核请假申请");
    titleLabel->setStyleSheet("font-size: 16pt; font-weight: bold;");
    mainLayout->addWidget(titleLabel, 0, Qt::AlignCenter);

    // 创建表格
    m_leaveRequestsTable = new QTableWidget;
    m_leaveRequestsTable->setSelectionBehavior(QAbstractItemView::SelectRows);
    m_leaveRequestsTable->setSelectionMode(QAbstractItemView::SingleSelection);
    m_leaveRequestsTable->setEditTriggers(QAbstractItemView::NoEditTriggers);

    // 设置表头
    QStringList headers;
    headers << "申请ID" << "员工ID" << "请假类型" << "开始日期" << "结束日期" << "请假原因" << "申请时间";
    m_leaveRequestsTable->setColumnCount(headers.size());
    m_leaveRequestsTable->setHorizontalHeaderLabels(headers);

    // 设置列宽
    m_leaveRequestsTable->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    m_leaveRequestsTable->horizontalHeader()->setSectionResizeMode(5, QHeaderView::ResizeToContents); // 请假原因列自适应宽度

    mainLayout->addWidget(m_leaveRequestsTable);

    // 创建按钮布局
    QHBoxLayout *buttonLayout = new QHBoxLayout;

    m_approveButton = new QPushButton("同意");
    m_rejectButton = new QPushButton("拒绝");
    m_refreshButton = new QPushButton("刷新");

    buttonLayout->addWidget(m_approveButton);
    buttonLayout->addWidget(m_rejectButton);
    buttonLayout->addWidget(m_refreshButton);

    mainLayout->addLayout(buttonLayout);

    // 连接按钮信号
    connect(m_approveButton, &QPushButton::clicked, this, &LeaveRecordView::onApproveButtonClicked);
    connect(m_rejectButton, &QPushButton::clicked, this, &LeaveRecordView::onRejectButtonClicked);
    connect(m_refreshButton, &QPushButton::clicked, this, &LeaveRecordView::loadPendingLeaveRequests);
}

void LeaveRecordView::loadPendingLeaveRequests()
{
    // 清空表格
    m_leaveRequestsTable->setRowCount(0);

    // 获取待审核的请假申请
    QList<QVariantList> leaveRequests = m_adminManager->getPendingLeaveRequests();

    // 填充表格
    for (int i = 0; i < leaveRequests.size(); ++i) {
        const QVariantList &request = leaveRequests[i];
        int row = m_leaveRequestsTable->rowCount();
        m_leaveRequestsTable->insertRow(row);

        for (int j = 0; j < request.size(); ++j) {
            QTableWidgetItem *item = new QTableWidgetItem(request[j].toString());
            m_leaveRequestsTable->setItem(row, j, item);
        }
    }
}

void LeaveRecordView::onApproveButtonClicked()
{
    // 获取选中的行
    int selectedRow = m_leaveRequestsTable->currentRow();
    if (selectedRow < 0) {
        QMessageBox::warning(this, "警告", "请先选择一条请假申请");
        return;
    }

    // 获取申请ID
    QString requestId = m_leaveRequestsTable->item(selectedRow, 0)->text();

    // 确认对话框
    if (QMessageBox::question(this, "确认", QString("确定要同意申请ID为 %1 的请假申请吗?").arg(requestId),
                              QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) {
        // 同意请假申请
        m_adminManager->approveLeaveRequest(requestId);
    }
    //QString emp_id= m_adminManager->getEmpIdByRequestId(requestId,&m_dbOperator) ;
    m_adminManager-> processEvaluationLeaveCount(requestId);
}

void LeaveRecordView::onRejectButtonClicked()
{
    // 获取选中的行
    int selectedRow = m_leaveRequestsTable->currentRow();
    if (selectedRow < 0) {
        QMessageBox::warning(this, "警告", "请先选择一条请假申请");
        return;
    }

    // 获取申请ID
    QString requestId = m_leaveRequestsTable->item(selectedRow, 0)->text();

    // 确认对话框
    if (QMessageBox::question(this, "确认", QString("确定要拒绝申请ID为 %1 的请假申请吗?").arg(requestId),
                              QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) {
        // 拒绝请假申请
        m_adminManager->rejectLeaveRequest(requestId);
    }
}
